package com.dl7.mvp;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Environment;
import android.os.IBinder;

import com.baidu.mobads.AdView;
import com.danikula.videocache.HttpProxyCacheServer;
import com.danikula.videocache.file.FileNameGenerator;
import com.dl7.mvp.api.RetrofitService;
import com.dl7.mvp.music.service.MusicService;
import com.dl7.mvp.utils.PreferencesUtils;
import com.dl7.mvp.utils.Utils;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class XApp extends Application {
    private static Context mContext = null;
    // 边播边存
    private HttpProxyCacheServer proxy;
    private static ServiceConnection mServiceConnection;
    private static MusicService.PlayerBinder mBinder;
    private static ServiceBindListener mListener;

    public interface ServiceBindListener {
        void onServiceBinded(MusicService.PlayerBinder binder);
        void onServiceUnBinded();
    }
    @Override
    public void onCreate() {
        mContext = this;
        super.onCreate();

        RetrofitService.init();
        Logger.init("12345");

        AdView.setAppSid(this,	getAppID());//第二个参数需改为您的 APPID
    }

    public static Context getContext() {
        return mContext;
    }

    /**
     * VideoCache
     */
    public HttpProxyCacheServer getProxy() {
        if (proxy == null) {
            proxy = newProxy();
        }
        return proxy;
    }

    private HttpProxyCacheServer newProxy() {
        Logger.i("path: " + getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath());
        return new HttpProxyCacheServer.Builder(this)
                .cacheDirectory(getExternalFilesDir(Environment.DIRECTORY_MOVIES))
                .maxCacheSize(1024 * 1024 * 1024)
                .fileNameGenerator(new MyFileNameGenerator())
                .build();
    }

    public class MyFileNameGenerator implements FileNameGenerator {
        public String generate(String url) {
            return Utils.clipFileName(url);
        }
    }

    public static boolean showAD() {
        Calendar calendar = Calendar.getInstance();
        int curYear = calendar.get(Calendar.YEAR);
        int curMonth = calendar.get(Calendar.MONTH) + 1;
        int curDay = calendar.get(Calendar.DAY_OF_MONTH);
        return curYear >= 2019 && curMonth >= 1 && curDay >= 17;
    }

    public static String getAppID() {
        return "e4832a06";
    }

    public static String getBannerID() {
        return "4762756";
    }

    public static void bindMusicService(ServiceBindListener listener) {
        mListener = listener;
        if (mServiceConnection == null) {
            mServiceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    mBinder = (MusicService.PlayerBinder) service;
                    listener.onServiceBinded(mBinder);
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    mBinder = null;
                }
            };
            Intent intent = new Intent(getContext(), MusicService.class);
            mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
        } else {
            if (mBinder != null) {
                listener.onServiceBinded(mBinder);
            }
        }
    }

    public static void unbindMusicService() {
        if (mServiceConnection != null) {
            if (mListener != null) {
                mListener.onServiceUnBinded();
            }
            mContext.unbindService(mServiceConnection);
            mContext.stopService(new Intent(getContext(), MusicService.class));
            mServiceConnection = null;
        }
    }
}
